 /**
  * Multer是Express官方推出的，用于Node.jsmultipart/form-data请求数据处理的中间件。
  */
module.exports = app =>{
    const fs = require('fs')
    const multer = require('multer')
    const upload = multer({
        dest: __dirname + '/../uploads'
    }).single('file')

    app.post('/admin/api/upload', upload, async (req, res) => {
        const file = req.file
        let filename = file.path.lastIndexOf("\\")
        let filelenght = file.path.length
        let newFilename = file.path.substring(0,filename + 1)
   
        newFilename =  newFilename + file.originalname
        console.log(file.path)
        console.log(newFilename)
        fs.rename(file.path, newFilename, function (err) {
            if (err) {
                throw err;
            }
            console.log('上传图片ok');
        })
        file.url = `http://localhost:3000/uploads/${file.originalname}`
        res.send(file)
    })
}